package com.space.school.dao.impl;

import com.space.school.dao.ProductListDao;
import com.space.school.domain.ProductCategory;
import com.space.school.domain.User;
import com.space.school.utils.SpaceJDBCUtils;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;

import java.util.*;

public class ProductListDaoImpl implements ProductListDao {
    private JdbcTemplate jdbcTemplate=new JdbcTemplate(SpaceJDBCUtils.getDataSource());
    @Override
    public int count(Map<String,String[]> map) {
        String sql = "select count(*) from product_category";
        return jdbcTemplate.queryForObject(sql, Integer.class);
    }
    @Override
    public List<ProductCategory> search(Map<String, String[]> map, int start, int pageSize) {
        StringBuffer sql=new StringBuffer("select * from product_category where 99=99 ");
        //        取值的集合
        ArrayList<Object> params=new ArrayList<>();
//        keySet 取map中的建的数组
//        [{用户名={11}},{生效表示={生效}}]
        Set<String> set=map.keySet();
        for(String key:set){
            if(!map.get(key)[0].equals("") && !key.equals("currentPage")){
                sql.append(" and "+key+ " like ?" );
                params.add("%"+map.get(key)[0]+"%");
            }

        }
        params.add(start);
        params.add(pageSize);
        sql.append(" order by category_id limit ?,?");
        List<ProductCategory>A=jdbcTemplate.query(sql.toString(), new BeanPropertyRowMapper<>(ProductCategory.class), params.toArray());
        return A;
    }

    @Override
    public void del(int category_id) {
        String sql = "delete from product_category where category_id=?";
        jdbcTemplate.update(sql,category_id);
    }

    @Override
    public List<ProductCategory> find(int category_id) {
        String sql = "select * from product_category where category_id = ?";
        return jdbcTemplate.query(sql, new BeanPropertyRowMapper<ProductCategory>(ProductCategory.class),category_id);
    }

    @Override
    public void update(ProductCategory productCategory) {
        String sql="UPDATE product_category SET category_name = ? ,category_type=?,update_time=? WHERE category_id = ?";
        jdbcTemplate.update(sql,productCategory.getCategory_name(),productCategory.getCategory_type(),new Date(),productCategory.getCategory_id());
    }
//    新增
    @Override
    public int insert(String category_name, String category_type) {
//        判断名字是否重复 如果重复 返回1
        String sql_category_name="select category_name from product_category where category_name=?";
        List<ProductCategory> user_list= jdbcTemplate.query(sql_category_name,new BeanPropertyRowMapper<>(ProductCategory.class),category_name);
        if(user_list.size()!=0){
            return 1;
        }
//        判断编号是否重复 如果重复 返回2
//        如果能成功新增 则返回3
        String sql = "insert into product_category (category_name, category_name,create_time) values (?, ?, ?)";
        jdbcTemplate.update(sql, category_name, category_name,new Date());
        return 3;
    }

    @Override
    public  List<ProductCategory> findAll() {
        String sql = "select * from product_category";
        List<ProductCategory>A=jdbcTemplate.query(sql, new BeanPropertyRowMapper<>(ProductCategory.class));
        return A;
    }
}
